package com.yjc.drygoods.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.member.entity.MemberReceiveAddressEntity;
import com.yjc.drygoods.member.interceptor.LoginUserInterceptor;
import com.yjc.drygoods.member.vo.FareVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.member.service.MemberReceiveAddressService;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 会员收货地址
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:16:26
 */
@Slf4j
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 删除地址时，保证一定有一个默认地址
     *
     * @param addressId
     */
    @GetMapping("/delete/address/{addressId}")
    public void deleteAddress(@PathVariable("addressId") Long addressId) {
        memberReceiveAddressService.deleteAddressAndKeepDefault(addressId);

    }

    /**
     * 根据地址id更改当前id地址为默认地址
     *
     * @param addressId
     * @return
     */
    @GetMapping("/update/defaultAddress/{addressId}")
    public void updateDefaultAddress(@PathVariable("addressId") Long addressId) {
        memberReceiveAddressService.updateDefaultAddress(addressId);
    }

    /**
     * 获取运费(运费要根据地址远近进行升降，但此时为了简单取了手机号最后一位作为运费)
     *
     * @param addrId
     * @return
     */
    @GetMapping("/getFare")
    public R getFare(@RequestParam("addrId") Long addrId) {
        FareVo fare = memberReceiveAddressService.getFare(addrId);
        return R.ok().setData(fare);
    }

    /**
     * 根据用户id获取该用户所有收货地址
     *
     * @param memberId
     * @return
     */
    @GetMapping("/getMember/address")
    public R getMemberAddress(@RequestParam("memberId") Long memberId) {
        List<MemberReceiveAddressEntity> addresses = memberReceiveAddressService.getMemberAddress(memberId);
        return R.ok().setData(addresses);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestParam("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        log.info("保存成功...{}", memberReceiveAddress);
        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
        memberReceiveAddress.setMemberId(memberRespVo.getId());
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
